package com.xiaoxin.yixinai.role;

import java.util.List;

import javax.persistence.EntityManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xiaoxin.yixinai._frame.base.BaseRepository;
import com.xiaoxin.yixinai._frame.base.BaseServiceProxy;

@Transactional(readOnly = true)
@Service
public class RoleService extends BaseServiceProxy<RoleInfo> {

    @Autowired
    private RoleDao roleDao;
    
    public List<RoleInfo> getRoleByUser(int userId) {
        return roleDao.findRolesByUserId(userId, 1);
    }
    /**
     * 获取角色列表
     */
    public List<RoleInfo> getRoleList(){
        return roleDao.getRoleList();
    }

    public Boolean isExistRoleCode(String roleCode){
        return roleDao.isExistRoleCode(roleCode) > 0;
    }

    public Boolean isExistRoleName(String roleName){
        return roleDao.isExistRoleName(roleName) > 0;
    }

    @Transactional
    public void saveOrUpdate(RoleInfo entity) {
        super.save(entity);
    }

    @Transactional
    public void saveOrUpdate(Iterable<RoleInfo> entities) {
        super.save(entities);
    }

    /**
     * 查询某个公司下的指定角色编码的对象
     * @param roleCode 角色编码
     * @param companyId 公司ID
     * @return
     */
    public RoleInfo findByRoleCodeAndCompanyId(String roleCode, Integer companyId) {
        return roleDao.findByRoleCodeAndCompanyId(roleCode, companyId);
    }

    @Override
    protected BaseRepository<RoleInfo> getBaseRepository() {
        return this.roleDao;
    }
}
